<?php 
## include required files
/*******************************/
require_once '../../includefiles.php';
require_once '../../model/users.php';
require_once '../../model/email.php';
include_once("../../model/trade.php");
include_once("../../model/budget.php");
require_once "../../includes/classes/paging_htacess.php";
require_once '../../redirect.php';
require_once '../../model/state.php';

## creating objects of required models
$userObj 		= new Model_Users();
$tradeObj = new Model_TRADE();
$budgetObj = new Model_Budget();
$stateObj = new Model_State();

checkUserLogin();
if($_SESSION['user_type'] == '4')
{
	@header("location:".SITE_URL."/");
	exit;
}


$userId =$_SESSION['getbids_id'];

## When user wants to delete the job
if($_GET['id1'] == 'delete' && $_GET['id2']!='')
{
	$jobsID = base64_decode($_GET['id2']);
	$userObj->deleteJobById($jobsID);
	$userObj->deleteUserJobViewByJOBId($jobsID);
	$_SESSION['msg'] = "<div class='success_msg'style='margin-top:5px;margin-bottom: 20px;'><span>Job deleted successfully !</span></div>";
	header("Location:".SITE_URL."/jobs/1/myjobs/");
	exit;
}
## When user wants to status change 
if($_GET['id1'] == 'Accept' && $_GET['id2']!='')
{
	
	$jobsID = base64_decode($_GET['id2']);
	
	$userjobArray = array();
	$userjobArray['project_status']	= 'Completed';
	$userObj->editJobPostValueByJOBId($userjobArray,$jobsID);	
	
	$fetchjobviewdetail = $userObj->getJobviewDetailsWithAccept($jobsID);
	
	$userjobviewArray = array();
	$userjobviewArray['status']	= 'Completed';	
	$userObj->editUserViewJobValueById($userjobviewArray,$fetchjobviewdetail[0]['id']);

	$_SESSION['msg'] = "<div class='success_msg'style='margin-top:5px;margin-bottom: 20px;'><span>Job status changed successfully !</span></div>";
	header("Location:".SITE_URL."/jobs/1/myjobs/");
	exit;
}



## for pagination
	$page = $_GET['id1'];
	if(!isset($_GET['id1']))
	$page = 1;
	else
	$page = $page;
	
## limit per pag 
$tetsimonialPerPage =10;
$StartRow = $tetsimonialPerPage  * ($page-1);
## Getting all jobs from database according to user

if($_POST['jobtype']!='0' && $_POST['jobtype']!='')
{
	$jobtype = $_POST['jobtype']; 
} 
elseif($_GET['id2']!='' && $_GET['id2']!='JOBTYPE')
{
	$jobtype =$_GET['id2']; 
}
else {
	$jobtype = '';
}
if($_POST['budget']!='0' && $_POST['budget']!=''){
	$budget = $_POST['budget'];
} 
elseif($_GET['id3']!='' && $_GET['id3']!='BUDGET')
{
	$budget =$_GET['id3']; 
}
else 
{
	$budget = '';
}
if($_POST['project_status']!='0' && $_POST['project_status']!=''){
	$Projectstatus = $_POST['project_status']; 
}
elseif($_GET['id4']!='' && $_GET['id4']!='PROJECTSTAUS')
{
	$Projectstatus =$_GET['id4']; 
}
else {
	$Projectstatus = '';
}

$jobsArray  = $userObj->JobsAccordingToUserId($userId,$jobtype,$budget,$Projectstatus,'','',false);
$totalCount        = count($jobsArray);
$smarty -> assign("recordsFound",$totalCount);

$result = $userObj->JobsAccordingToUserId($userId,$jobtype,$budget,$Projectstatus,$tetsimonialPerPage,$StartRow,true);
$show = 1;
$total_pages = ceil($totalCount / $tetsimonialPerPage);
	if($total_pages > 1)
	$smarty -> assign("showpgnation","yes");
	$showing    = !isset($_GET["id1"]) ? 1 : $page;
	
	## it shoould be file name 
	if(($_GET['id2']!='' || $_GET['id3']!='' || $_GET['id4']!='') || ($_POST['jobtype']!='' || $_POST['budget']!='' || $_POST['project_status']!=''))
	{
		$firstlink ='';
		if(($_POST['jobtype']!='0' || $_GET['id2']!='' )&& $_GET['id2']!='JOBTYPE')
			$firstlink  .= $jobtype."/";
		else	
			$firstlink  .= "JOBTYPE/";
			
		if(($_POST['budget']!='0' || $_GET['id3']!='' )&& $_GET['id3']!='BUDGET')
			$firstlink  .= $budget."/";
		else	
			$firstlink  .= "BUDGET/";

		if(($_POST['project_status']!='0' || $_GET['id4']!='') && $_GET['id4']!='PROJECTSTAUS')
			$firstlink  .= $Projectstatus."/";
		else	
			$firstlink  .= "PROJECTSTAUS/";
			
		
		$firstlink .= "myjobs/";	
	}
	else
		$firstlink  = "myjobs/";	
	
	$baselink   = $firstlink;
	if($total_pages!=0)
		$pagenation = pagination($total_pages, $show, $showing, $firstlink, $baselink, "", "jobs");
	$smarty -> assign("pagenation", $pagenation);
	$smarty -> assign("page", $page);
	//
		
	
	for($i=0;$i<count($result);$i++)
	{
		if($result[$i]['trade_id']!='')
		{
			$gettradedetail = $tradeObj->getTradeCategoryDetailsById($result[$i]['trade_id']);
			$result[$i]['tradename'] = $gettradedetail['trade_category'];
		}
		
		if($result[$i]['jobtype_id']!='')
		{
			$getjobtypedetail = $tradeObj->getTradeCategoryDetailsById($result[$i]['jobtype_id']);
			$result[$i]['jobtypename'] = $getjobtypedetail['trade_category'];
		}
		
		if($result[$i]['approx_budget']!='')
		{
			$getbudgetdetail = $budgetObj->getBudgetDetailsById($result[$i]['approx_budget']);
			$result[$i]['budgetname'] = $getbudgetdetail['budget_name'];
		}
		
		if($result[$i]['postjob_id']!='')
		{
			$getleadcount = $userObj->getleadcountofTradesmanByJobId($result[$i]['postjob_id']);
			$result[$i]['countofleads'] = $getleadcount['cnt'];
			
			//code for review
			$jobview_detail = $userObj->getUserViewjobDetailsByJIDANDSTATUS($result[$i]['postjob_id'],'Completed');
			$accepted_user =$jobview_detail['userid'];
			if($accepted_user!='')
			{
				$reviewdetail =$userObj->getReviewdetailByFromUIDToUIDJobID($result[$i]['postjob_id'],$userId,$accepted_user);
				$result[$i]['reviewid'] = $reviewdetail['id'];
			}

		}		
		
		if($result[$i]['state_id']!=''){
		$State_array = $stateObj->getStateDetailsById($result[$i]['state_id']);
		$result[$i]['state_name'] = $State_array['state_name'];
		}
		
	}

	$smarty->assign("jobsArray", $result);
	
/*===========================================================================*/
	$userDetArray = $userObj->getUserDetailsByUserId($userId);
	$smarty->assign("userDetArray", $userDetArray);	
	
/*===========================================================================*/	

$getallTradeJobType = $tradeObj->getAllJobTypeWithActiveStatus();
$smarty->assign("getallTradeJobType", $getallTradeJobType);	

$getallBudget = $budgetObj->getAllBudgetWithActiveStatus();
$smarty->assign("getallBudget", $getallBudget);	


if($_SESSION['msg']){
		$smarty->assign("msg", $_SESSION['msg']);
		unset($_SESSION['msg']);
}

$active ="myaccount";
$smarty->assign('active',$active);
$subactive ="myjobs";
$smarty->assign('subactive',$subactive);
$smarty->assign('mainmenu',"4");

$smarty->display(TEMPLATEDIR.'/modules/jobs/myjobs.tpl');
unset($smarty);
?>
